<script setup lang="ts">
  import { useUpdateUserCheck } from '@/services/pages/code/useCode'
  import { HMessage, HMessageBox } from '@/components/common/message'

  const { checkFrom, checkLoading, resetForm, check } = useUpdateUserCheck()

  defineOptions({
    name: 'CheckUpdateUserModal',
  })

  const props = defineProps({
    recordId: {
      type: String,
      required: true,
    },
  })

  const emits = defineEmits<{
    (e: 'update'): void
  }>()

  // 弹窗的显示状态
  const visible = defineModel('modelValue', { type: Boolean, default: false })

  /**
   * 隐藏弹窗
   */
  const hideDialog = () => {
    resetForm()
    visible.value = false
  }

  /**
   * 审核事件
   * @param {(1|2)} checkResult - 审核结果
   */
  const handleCheck = async (checkResult: 1 | 2) => {
    checkFrom.value.applyState = checkResult
    checkFrom.value.id = props.recordId
    const res = await check()
    if (res.code === 20000) {
      HMessage.success('审核成功')
      emits('update')
      hideDialog()
      return
    }

    HMessageBox.error(res.message)
  }
</script>

<template>
  <h-dialog v-model="visible" title="公司变更审核" :loading="checkLoading" @close="hideDialog">
    <el-form inline-message label-position="top" :model="checkFrom" class="modal_form_content">
      <el-form-item label="备注">
        <el-input
          v-model="checkFrom.applyMsg"
          type="textarea"
          placeholder="请输入备注"
          maxlength="200"
          show-word-limit
        />
      </el-form-item>
    </el-form>

    <template #footer>
      <h-button type="primary" @click="handleCheck(1)" is-modal>通过</h-button>
      <h-plain-button @click="handleCheck(2)" is-modal type="error">驳回</h-plain-button>
    </template>
  </h-dialog>
</template>

<style scoped lang="scss">
  :deep(.el-textarea__inner) {
    height: 100px;
  }
</style>
